﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Data;
using System.Xml.Linq;

namespace Twi.COMMON.WPF.Ctl
{
    public class TreeGrid
    {

    }

    public class TreeItem : INotifyPropertyChanged
    {
        public TreeItem(XElement xeItem)
        {
            this._xeItem = xeItem;
        }

        private XElement _xeItem;

        /// <summary>
        /// 数据行
        /// </summary>
        public XElement XeItem
        {
            get
            {
                return _xeItem;
            }
            set
            {
                _xeItem = value;
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("XeItem"));
            }
        }

        private int _level;

        /// <summary>
        /// 层级
        /// </summary>
        public int Level
        {
            get
            {
                return _xeItem.Element("Level").GetValue<int>(0);
            }
            set
            {
                _level = value;
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("Level"));
            }
        }

        private ObservableCollection<TreeItem> _children = new ObservableCollection<TreeItem>();
        public ObservableCollection<TreeItem> Children
        {
            get { return _children; }
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }

    public class LevelToMarginConverter : IValueConverter
    {
        public object Convert(object o, Type type, object parameter,
                              CultureInfo culture)
        {
            return new Thickness(int.Parse(o.ToString()) * c_IndentSize, 0, 0, 0);
        }

        public object ConvertBack(object o, Type type, object parameter,
                                  CultureInfo culture)
        {
            throw new NotSupportedException();
        }

        private const double c_IndentSize = 15.0;
    }
}
